﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EbbinghausWord.BLL;
using EbbinghausWord.Common;
using Microsoft.Phone.Controls;

namespace EbbinghausWord.Pages
{
    public partial class Option : PhoneApplicationPage
    {
        public Option()
        {
            InitializeComponent();

            _voiceManager.MuliDownloadPercentaging += (s, ex) => Dispatcher.BeginInvoke(() => RefreshDownloadProgress(ex.HaveDownloadCount));

            _voiceManager.MultiDownloadVoiceCompleted +=
                (s, ex) => Dispatcher.BeginInvoke(() =>
                            {
                                //下载完成
                                //RefreshDownloadProgress();
                                downloadVoiceButton.Content = "开始下载";
                                _isDownload = !_isDownload;
                                Tools.ShowMessage("下载完成");
                            });
        }

        #region private field
        /// <summary>
        /// 是否正在下载
        /// </summary>
        private bool _isDownload = false;
        private readonly VoiceManager _voiceManager = new VoiceManager();
        #endregion

        #region private method
        /// <summary>
        /// 刷新声音下载进度
        /// </summary>
        /// <param name="downloadFileCount"></param>
        private void RefreshDownloadProgress(int downloadFileCount)
        {
            voiceCountTextBlock.Text = string.Format(
                @"{0}/{1}", downloadFileCount, //VoiceManager.GetVoiceFileCount(),
                VoiceManager.GetTotalCount());
        }

        /// <summary>
        /// 刷新声音下载进度
        /// </summary>
        /// <param name="downloadFileCount"></param>
        private void RefreshDownloadProgress()
        {
            voiceCountTextBlock.Text = string.Format(
                @"{0}/{1}", VoiceManager.GetVoiceFileCount(),
                VoiceManager.GetTotalCount());
        }

        #endregion

        #region callback

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            restore.IsEnabled = BackupManager.IsExistBackup();
            RefreshBackupTextBlock();

            RefreshDownloadProgress();

            this.DataContext = new UserConfig();

            //设置ToggleSwitch控件显示中文开关
            Tools.SetToggleSwitchWithChinese(autoPlayVoiceToggleSwitch);
        }

        /// <summary>
        /// 刷新备份时间
        /// </summary>
        private void RefreshBackupTextBlock()
        {
            backupTextBlock.Text = BackupManager.GetBackupDate();
        }

        private void backup_Click(object sender, RoutedEventArgs e)
        {
            BackupManager.BackupWorkbook();
            Tools.ShowMessage("学习记录备份成功！");
            restore.IsEnabled = BackupManager.IsExistBackup();
            RefreshBackupTextBlock();
        }

        private void restore_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("恢复学习记录将您的学习记录恢复到上次执行备份的时刻，同时会导致当前的学习进度丢失，确认恢复吗？", "提示", MessageBoxButton.OKCancel)
                == MessageBoxResult.OK)
            {
                BackupManager.ResotreWorkbook();
                Tools.ShowMessage("恢复学习记录成功！");
            }
        }

        private void clearCache_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("清除学习记录将清除您所有的学习记录，导致当前的学习进度丢失，确认清除吗？", "提示", MessageBoxButton.OKCancel)
                           == MessageBoxResult.OK)
            {
                WordBookManager.ClearWorkbook();
                Tools.ShowMessage("清除学习记录成功！");
            }
        }

        private void orderButton_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("顺序排列将您当前的单词本按顺序重建，该操作不会造成学习进度丢失，确认顺序排列吗？", "提示", MessageBoxButton.OKCancel)
                          == MessageBoxResult.OK)
            {
                WordBookManager.OrderWordBook();
                //清除队列
                WordQueueManager.InitQueue();

                WordBookManager.IsNeedReflesh = true;
                Tools.ShowMessage("顺序排列完成！");
            }

        }

        private void unorderButton_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("乱序排列将您当前的单词本按乱序重建，该操作不会造成学习进度丢失，确认乱序排列吗？", "提示", MessageBoxButton.OKCancel)
                        == MessageBoxResult.OK)
            {
                WordBookManager.UnorderWordBook();
                //清除队列
                WordQueueManager.InitQueue();

                WordBookManager.IsNeedReflesh = true;
                Tools.ShowMessage("乱序排列完成！");
            }
        }

        private void downloadVoiceButton_Click(object sender, RoutedEventArgs e)
        {
            if (_isDownload)
            {
                if (MessageBox.Show("停止下载后，可能会导致您下次下载的速度变慢，是否继续？", "停止下载单词发音", MessageBoxButton.OKCancel)
                    == MessageBoxResult.OK)
                {
                    downloadVoiceButton.Content = "开始下载";

                    //停止下载
                    _voiceManager.StopDownloadVoice();
                }
            }
            else
            {
                if (MessageBox.Show("下载单词发音大约会占用您几十兆的流量，建议开启Wi-Fi或连接电脑后下载，您是否继续？", "下载单词发音", MessageBoxButton.OKCancel)
                   == MessageBoxResult.OK)
                {
                    downloadVoiceButton.Content = "停止下载";
                    //开始下载
                    _voiceManager.StartDownloadVoice();
                }
            }


            _isDownload = !_isDownload;
        }
        #endregion
    }
}